package cn.ujn.medicalbackend.controller;

import cn.ujn.medicalbackend.common.BaseResponse;
import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.common.ResultUtils;
import cn.ujn.medicalbackend.domain.CompanyPolicy;
import cn.ujn.medicalbackend.dto.CompanyPolicyQuery;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.CompanyPolicyAddRequest;
import cn.ujn.medicalbackend.request.CompanyPolicyDeleteRequest;
import cn.ujn.medicalbackend.request.CompanyPolicyUpdateRequest;
import cn.ujn.medicalbackend.service.CompanyPolicyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/companyPolicy")
@CrossOrigin(origins = {"http://localhost:9092"})
public class CompanyPolicyController {
    @Autowired
    private CompanyPolicyService companyPolicyService;

    @GetMapping("/list/companyPolicy")
    public BaseResponse<Page<CompanyPolicy>> getcompanyPolicyListByPage(CompanyPolicyQuery companyPolicyQuery) {
        if (companyPolicyQuery== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        CompanyPolicy companyPolicy = new CompanyPolicy();
        BeanUtils.copyProperties(companyPolicyQuery, companyPolicy);
        QueryWrapper<CompanyPolicy> queryWrapper = new QueryWrapper<>(companyPolicy);
        Page<CompanyPolicy> page = new Page<>(companyPolicyQuery.getPageNum(), companyPolicyQuery.getPageSize());
        Page<CompanyPolicy> tempPage = companyPolicyService.page(page, queryWrapper);
        Page<CompanyPolicy> resultPage = companyPolicyService.companyPolicyPageTransfer(tempPage);
        return ResultUtils.success(resultPage);
    }
    @PostMapping("/add")
    public BaseResponse<Long> addCompanyPolicy(@RequestBody CompanyPolicyAddRequest companyPolicyAddRequest, HttpServletRequest request) {
        if (companyPolicyAddRequest== null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        long companyPolicyId =companyPolicyService.addCompanyPolicy(companyPolicyAddRequest, request);
        return ResultUtils.success(companyPolicyId);
    }

    @PostMapping("/update")
    private BaseResponse<Boolean> updateCompanyPolicy(@RequestBody CompanyPolicyUpdateRequest companyPolicyUpdateRequest, HttpServletRequest request) {

        if (companyPolicyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = companyPolicyService.updateCompanyPolicy(companyPolicyUpdateRequest,request);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    private BaseResponse<Boolean> deleteCompanyPolicy(@RequestBody CompanyPolicyDeleteRequest companyPolicyDeleteRequest, HttpServletRequest request) {
        if (companyPolicyDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result =  companyPolicyService.deleteCompanyPolicy(companyPolicyDeleteRequest, request);
        return ResultUtils.success(result);
    }
}
